<?php

include_once("Model/tinhthanh_model.php");

class TinhThanh extends MyController
{    
    public $model = null;
    
    public function __construct()
    {
        $this->model = new TinhThanh_Model();
    }
    
    public function addView($note)
    {
        $notice = $note;
        include("View/header.php");
        include("View/tinhthanh/add.php");
        include("View/footer.php");
    }
    
    public function getAllView($note)
    {
        $notice = $note;
        $rows = $this->model->getAllTinhThanh();
        include("View/header.php");
        include("View/tinhthanh/list.php");
        include("View/footer.php");
    }
    
    public function editView($maTinhThanh, $note)
    {
        $notice = $note;
        $row = mysql_fetch_array($this->model->getTinhThanh($maTinhThanh));
        include("View/header.php");
        include("View/tinhthanh/edit.php");
        include("View/footer.php");
    }
    
    public function getAll()
    {
        $this->getAllView("");        
    }    
    
    public function add()
    {
        if(isset($_POST['submit']))
        {
            $maTinhThanh = $_POST['matinhthanh'];
            $tenTinhThanh = $_POST['tentinhthanh'];
            $task = $_POST['task'];
            
            if($this->model->checkExist($task, $maTinhThanh, $tenTinhThanh))
            {                
                $notice = Message::$addFail;
                $this->addView($notice);
                
            }
            else
            {
                $this->model->addTinhThanh($maTinhThanh, $tenTinhThanh);
                $notice = Message::$addSuccess;                
                $this->addView($notice);                
            }
        }
        else
        {
            $notice = "";
            $this->addView($notice);
        }
    }
    
    public function edit()
    {
        if(isset($_GET['matinhthanh']))
        {            
            $maTinhThanh = $_GET['matinhthanh'];
            if(isset($_POST['submit']))
            {
                $tenTinhThanh = $_POST['tentinhthanh'];
                $task = $_POST['task'];
                $result = mysql_fetch_array($this->model->getTinhThanh($maTinhThanh));
                if($tenTinhThanh == $result['tentinhthanh'])
                {
                    $notice = Message::$youDoNothing;
                    $this->getAllView($notice);
                }
                else 
                {
                    if($this->model->checkExist($task, $maTinhThanh, $tenTinhThanh))
                    {
                        $notice = Message::$editFail;
                        $this->editView($maTinhThanh, $notice);
                    }
                    else
                    {
                        $this->model->editTinhThanh($maTinhThanh, $tenTinhThanh);
                        $notice = Message::$editSuccess;
                        $this->getAllView($notice);
                    }
                }
            }
            else
            {
                $this->editView($maTinhThanh, "");
            }
        }
        else
        {
            $this->getAllView("");
        }
    }
    
    public function delete()
    {
        if(isset($_GET['matinhthanh']))
        {
            if($this->model->checkTinhThanhStatus($_GET['matinhthanh']))
            {
                $notice = Message::$deleteFail;
                $this->getAllView($notice);
            }
            else
            {
                $this->model->deleteTinhThanh($_GET['matinhthanh']);
                $notice = Message::$deleteSuccess;
                $this->getAllView($notice);
            }
        }
        else
        {
            $this->getAllView("");
        }
    }
    
}

?>